package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.AppInfo;
import kd.bos.entity.AppMetadataCache;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.param.AppCustomParam;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.parameter.SystemParamServiceHelper;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;

//资金计划和资金计划调整提交校验：校验计划收付款日期是否在年月的月范围内
public class FundPlanSubmitOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("qeug_yearmonth");//年月
        e.getFieldKeys().add("qeug_billtype");//单据类型
        e.getFieldKeys().add("entryentity");//明细分录
        e.getFieldKeys().add("qeug_planpaydate");//计划收付款日期
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.getValidators().add(new ValidatorExt());
    }

    class ValidatorExt extends AbstractValidator {
        @Override
        public void validate() {
            ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
            for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
                DynamicObject dataEntity = extendedDataEntity.getDataEntity();
                DynamicObject billType = dataEntity.getDynamicObject("qeug_billtype");//单据类型
                if (billType == null) {
                    continue;
                }
                String billTypeNumber = billType.getString("number");
                if (billTypeNumber.equals("qeug_fundplan_01") || billTypeNumber.equals("qeug_fundplan_02") || billTypeNumber.equals("qeug_fundadjust_01") || billTypeNumber.equals("qeug_fundadjust_02")) {
                    //月度支出计划和月度收入计划校验
                    QFilter[] qFilter = new QFilter[]{
                            new QFilter("qeug_deparment.id", QCP.equals, dataEntity.getDynamicObject("qeug_deparment").get("id")),//部门id
                            new QFilter("billstatus", QCP.equals, "C").or(new QFilter("billstatus", QCP.equals, "B")),//已提交或已审核单据
                            new QFilter("qeug_yearmonth", QCP.equals, dataEntity.getDate("qeug_yearmonth"))//年月
                    };
                    DynamicObject zijin = BusinessDataServiceHelper.loadSingle(dataEntity.getDynamicObjectType().getName(), qFilter);//资金计划和资金计划调整
                    if (zijin != null) {
                        this.addFatalErrorMessage(extendedDataEntity, "对应部门已存在对应年月的资金计划");
                        continue;
                    }
                    Date qeug_yearmonth = dataEntity.getDate("qeug_yearmonth");//年月
                    Date date = new Date();//当前时间
                    if (qeug_yearmonth == null) {
                        continue;
                    }
                    LocalDate localDate = qeug_yearmonth.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    LocalDate localDate1 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    int yearMonth = localDate.getMonthValue();//月份
                    int monthValue = localDate1.getMonthValue();//当前月份

                    AppInfo cgfwAppInfo = AppMetadataCache.getAppInfo("em");//获取配置的appId
                    String id = cgfwAppInfo.getId();
                    AppCustomParam appCustomParam = new AppCustomParam(id);
                    Map<String, String> stringStringMap = SystemParamServiceHelper.loadAppCustomParameterFromCache(appCustomParam);//获取自定义参数配置数据
                    String qeug_fundplan = stringStringMap.get("qeug_fundplan");//参数名称
                    if (!qeug_fundplan.equals("true")) {
                        //不为1的时候，校验年月于当前月份是否相同，如果相同，则提示错误信息
                        if (yearMonth == monthValue) {
                            this.addFatalErrorMessage(extendedDataEntity, "配置中关闭可选当月报销，请开启后再提交");
                        }
                    }

                    DynamicObjectCollection entryEntityCollection = dataEntity.getDynamicObjectCollection("entryentity");//明细
                    for (int i = 0; i < entryEntityCollection.size(); i++) {
                        DynamicObject entryEntity = entryEntityCollection.get(i);
                        Date qeug_planpaydate = entryEntity.getDate("qeug_planpaydate");//计划收付款日期
                        if (qeug_planpaydate == null) {
                            continue;
                        }
                        LocalDate planPayDate = qeug_planpaydate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                        if (planPayDate.getMonthValue() != yearMonth) {
                            this.addFatalErrorMessage(extendedDataEntity, "第" + (i + 1) + "行，计划收付款日期不在年月的月份范围内");
                        }
                    }
                }
            }
        }
    }
}
